<?php
require_once(dirname(__FILE__)."/../lib/Loader.php");
cfgload("defines");
cload("Smarty.smartyRenderer");
cload("requestProcessor");

/**
 * Description of guestIndex
 *
 * @author Di_Moon
 */
class guestIndex extends smartyRenderer {
    protected $_dbh;
    protected $_user;
    protected $_action;
            
    function __construct($dbh) {
        parent::__construct();
        $this->_dbh = $dbh;
        
        $this->_user = _sessionUser();
        
        $this->_action = requestProcessor::getVar("action");
        
        if($this->_action=="logout") {
            user::logout();
            header("Location: ".$_SERVER["SCRIPT_NAME"]);
            exit();
        }
        
        if($this->_action=="login") {
            $post = requestProcessor::post();
                        
            if($this->_user->login($post->get("login"), $post->get("password"))) {
                header("Location: ".$_SERVER["SCRIPT_NAME"]);
                exit();
            }
        }
    }
    
    function render() {
        $page = requestProcessor::getVar("tpl", "guestIndex");

        // Гостям доступны только страницы начинающиеся с guest
        if($this->_user->guest()) {
            if(!preg_match("/^guest/", $page)) {
                $page = "guestRestrictedArea";
            }
        }
        
        return $this->fetch($page.".tpl");
    }
}

?>
